package com.shtel.paas.monitorcenter.system.management.user.management.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.shtel.paas.monitorcenter.system.management.user.management.entity.LogUserRoleEntity;
import com.shtel.paas.monitorcenter.system.management.user.management.entity.LogUserRolePermission;
import com.shtel.paas.monitorcenter.system.management.user.management.entity.XxlJobQrtzMenuInfo;
import com.shtel.paas.monitorcenter.system.management.user.management.mapper.RoleAndAuthorityManagementMapper;
import com.shtel.paas.monitorcenter.system.management.user.management.service.RoleAndAuthorityManagementService;
import com.shtel.paas.monitorcenter.util.Page;

/**
 * @author: zhangyy 2018年5月17日 monitorCenter--角色权限管理service接口实现类
 */
@Service
public class RoleAndAuthorityManagementServiceImpl implements
		RoleAndAuthorityManagementService {

	@Autowired
	RoleAndAuthorityManagementMapper roleAndAuthorityManagementMapper;

	/**
	 * 查询所有角色
	 * 
	 * @param roleName
	 *            ,page,size
	 * @return Page
	 */
	@Override
	public Page<LogUserRoleEntity> getRoles(String roleName, int page, int size) {
		// TODO Auto-generated method stub

		PageHelper.startPage(page, size);
		List<LogUserRoleEntity> selectRoles = this.roleAndAuthorityManagementMapper
				.selectRoles(roleName);
		Page<LogUserRoleEntity> pageContent = Page.build(selectRoles);
		return pageContent;
	}

	/**
	 * 创建角色
	 * 
	 * @param logUserRoleEntity
	 * @return Integer
	 */
	@Override
	public Integer createRole(LogUserRoleEntity logUserRoleEntity) {
		// TODO Auto-generated method stub

		Integer result = this.roleAndAuthorityManagementMapper
				.createRole(logUserRoleEntity);
		return result;
	}

	/**
	 * 更改角色权限
	 * 
	 * @param logUserRoleEntity
	 * @return Integer
	 */
	@Override
	public Integer changeRolePermissions(LogUserRoleEntity logUserRoleEntity) {
		// TODO Auto-generated method stub
		Integer result = this.roleAndAuthorityManagementMapper
				.updateRole(logUserRoleEntity);
		return result;
	}

	/**
	 * 单个角色删除
	 * 
	 * @param ids
	 * @return Integer
	 */

	@Override
	public Integer deleteRoleByIds(String ids) {
		// TODO Auto-generated method stub
		LogUserRolePermission lo = new LogUserRolePermission();
		LogUserRoleEntity logUserRoleEntity = new LogUserRoleEntity();
		lo.setRoleId(ids);
		this.roleAndAuthorityManagementMapper.deleteRoleByIdsDelLURP(lo);
		logUserRoleEntity.setId(ids);
		Integer result = this.roleAndAuthorityManagementMapper
				.deleteRoleByIds(logUserRoleEntity);
		return result;
	}

	/**
	 * 角色批量删除
	 * 
	 * @param emp
	 * @return Integer
	 */

	@Override
	public Integer deleteRoleBatch(List<String> emp) {
		// TODO Auto-generated method stub
		this.roleAndAuthorityManagementMapper.deleteRoleBatchDelLURP(emp);
		Integer result = this.roleAndAuthorityManagementMapper
				.deleteRoleBatch(emp);
		return result;
	}

	/**
	 * 查询menu列表整体
	 * 
	 * @param
	 * @return Object
	 */
	@Override
	public Object getMenus() {
		// TODO Auto-generated method stub
		// 获取数据库菜单数据
		List<XxlJobQrtzMenuInfo> menuVoList = this.roleAndAuthorityManagementMapper
				.selectMenuData();
		// 创建父节点菜单
		List<XxlJobQrtzMenuInfo> parentEntityList = new ArrayList<XxlJobQrtzMenuInfo>();
		// 创建子节点菜单
		List<XxlJobQrtzMenuInfo> childrenEntiyList = new ArrayList<XxlJobQrtzMenuInfo>();
		// 创建三级菜单
		List<XxlJobQrtzMenuInfo> treeMenuEntiyList = new ArrayList<XxlJobQrtzMenuInfo>();

		// 创建最终结果菜单
		List<Object> resultList = new ArrayList<Object>();
		// 获取节点数据
		for (XxlJobQrtzMenuInfo menuVo : menuVoList) {
			if (menuVo.getId() == menuVo.getParentId()) {
				// 获取父节点菜单
				parentEntityList.add(menuVo);
			} else {
				// 获取子节点菜单
				childrenEntiyList.add(menuVo);
			}
			if (menuVo.getParentId() == 0) {
				// 获取三级菜单
				treeMenuEntiyList.add(menuVo);
			}
		}
		// 创建结果菜单
		for (XxlJobQrtzMenuInfo parent : parentEntityList) {
			// 临时子菜单
			List<Object> childLis = new ArrayList<Object>();
			// 临时结果菜单
			LinkedHashMap<String, Object> temporaryMenu = new LinkedHashMap<String, Object>();
			for (XxlJobQrtzMenuInfo children : childrenEntiyList) {
				LinkedHashMap<String, Object> temporaryChildMenu = new LinkedHashMap<String, Object>();
				List<Object> treeMenuChild = new ArrayList<Object>();
				if (parent.getId() == children.getParentId()) {
					for (XxlJobQrtzMenuInfo tree : treeMenuEntiyList) {
						LinkedHashMap<String, Object> treeMenu = new LinkedHashMap<String, Object>();
						if (children.getId() == tree.getTreeMenuId()) {
							treeMenu.put("id", tree.getmId());
							treeMenu.put("name", tree.getName());
							treeMenu.put("children", tree.getChild());
							treeMenuChild.add(treeMenu);
						}

					}
					if (parent.getId() != children.getTreeMenuId()) {
						temporaryChildMenu.put("id", children.getmId());
						temporaryChildMenu.put("name", children.getName());
					}

					if (treeMenuChild != null && treeMenuChild.size() > 0) {
						temporaryChildMenu.put("children", treeMenuChild);
					} else {

					}
					childLis.add(temporaryChildMenu);
				}
			}
			temporaryMenu.put("id", parent.getmId());
			temporaryMenu.put("name", parent.getName());
			if (childLis.size() > 0 && childLis != null) {
				temporaryMenu.put("children", childLis);
			} else {

			}
			resultList.add(temporaryMenu);
		}
		// 转成json数组
		Object jsonArray = JSONArray.toJSON(resultList);
		return jsonArray;

	}

	/**
	 * 查询role列表整体
	 * 
	 * @param
	 * @return Object
	 */
	@Override
	public Object getRoleList() {
		// TODO Auto-generated method stub
		List<LogUserRoleEntity> roleList = this.roleAndAuthorityManagementMapper
				.selectRoles("");
		// 转成json数组
		Object jsonArray = JSONArray.toJSON(roleList);
		return jsonArray;
	}

}
